package com.chixing.service.impl;

import com.chixing.mapper.CustomerMapper;
import com.chixing.mapper.HotelMapper;
import com.chixing.mapper.MyorderMapper;
import com.chixing.pojo.Customer;
import com.chixing.pojo.Hotel;
import com.chixing.pojo.Myorder;
import com.chixing.pojo.MyorderExample;
import com.chixing.service.Orderreservice;
import com.chixing.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class OrderreserviceImpl implements Orderreservice {
    @Autowired
    private HotelMapper hotelMapper;
    @Autowired
    private MyorderMapper myorderMapper;
    @Autowired
    private CustomerMapper customerMapper;


    @Override
    public ServerResponse getOrderHotel(Integer id) {
        Hotel hotel = hotelMapper.selectByPrimaryKey(id);
        if (hotel !=null)
            return ServerResponse.getSuccess(hotel);
        return ServerResponse.getFail(null);
    }

    @Override
    public ServerResponse getMyOrder() {
        List<Myorder> MyorderList = myorderMapper.selectByExample(null);
        if (MyorderList != null && MyorderList.size()>0)
            return ServerResponse.getSuccess(MyorderList);
        return ServerResponse.getFail(null);
    }

    @Override
    public ServerResponse getOrderCusId(Integer cusId) {
        Customer customer = customerMapper.selectByPrimaryKey(cusId);
        if (customer !=null)
            return ServerResponse.getSuccess(customer);
        return ServerResponse.getFail(null);
    }

    @Override
    public ServerResponse save(Myorder myorder) {
        int rows = myorderMapper.insert(myorder);
        System.out.println("添加订单 service rowes:" + rows);
        if(rows > 0)
            return ServerResponse.saveSuccess(rows);
        else
            return ServerResponse.saveFail(null);
    }

    //获得我的最新添加的订单
    public  ServerResponse getMyNewOrder(String orderNum){
        MyorderExample example = new MyorderExample();
        example.createCriteria().andOrderNumberEqualTo(orderNum);
       Myorder myorder = myorderMapper.selectByExample(example).get(0);
       return ServerResponse.getFail(myorder);
    }

    @Override
    public ServerResponse updateOrderStatus(Integer orderId, Integer orderStatus) {
        Myorder myorder = new Myorder();
        myorder.setOrderId(orderId);
        myorder.setOrderStatus(orderStatus);

         int rows = myorderMapper.updateByPrimaryKey(myorder);
        return ServerResponse.updateSuccess(rows);
    }

}
